<?php
/**
 * 
 * 直播带货
 * Author wsyone wsyone@faxmail.com
 * Time:2020年5月13日上午10:21:56
 * Copyright:钦州市友加信息科技有限公司
 * site:https://www.jihexian.com
 */
namespace addons\JhxEdu\backend\modules\live\controllers;
use addons\JhxEdu\backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use addons\JhxEdu\common\models\live\Live;
use addons\JhxEdu\common\models\live\LiveSearch;
use addons\JhxEdu\api\common\controllers\XcxApi;
use Yii;
use yii\helpers\Json;
use yii\helpers\Url;
/**
 * Default controller for the `live` module
 */
class LiveController extends BaseController
{
    /**
     * Lists all Live models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LiveSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    
    public function actions()
    {
        return [
            'ajax-update-field' => [
                'class' => 'common\\actions\\AjaxUpdateFieldAction',
                'allowFields' => ['status'],
                'findModel' => [$this, 'findModel']
            ],
            'switcher' => [
                'class' => 'backend\widgets\grid\SwitcherAction'
            ],
            'position' => [
                'class' => 'backend\\actions\\Position',
                'returnUrl' => Url::current()
            ]
        ];
    }
    
    /**
     * Displays a single Live model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    
    public function actionMp4($id)
    {
        $model=$this->findModel($id);
        if($model['live_status']!=103||empty($model->live_replay)){
            yii::$app->session->setFlash('error','暂无数据');
            return $this->goBack();
        }
        return $this->render('mp4', [
            'model' =>Json::decode($model->live_replay)
        ]);
    }
    /**
     * Creates a new Live model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Live();
        $model->load(Yii::$app->request->post());
        $model->loadDefaultValues();
        $model->share_img=isset($model->share_img['path'])?$model->share_img['path']:'';
        
        if (!empty($model->url)){
            if(!$model->share_img){
                yii::$app->session->setFlash('error','请上传封面');
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
            $model->type ='url';
            if($model->save()){
                yii::$app->session->setFlash('success','创建成功');
            }else{
                yii::$app->session->setFlash('error',current($model->getFirstErrors()));
            }
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    
    /**
     * Updates an existing Live model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->load(Yii::$app->request->post());
        if ($model->load(Yii::$app->request->post())) {
            if(is_array($model->share_img)){
                $model->share_img=isset($model->share_img['path'])?$model->share_img['path']:'';
            }
            if(!$model->url){
                $model->url='noUrl';
            }
            if($model->share_img){
                if($model->save()){
                    yii::$app->session->setFlash('success','修改成功');
                }else{
                    yii::$app->session->setFlash('error',current($model->getFirstErrors()));
                }
            }else{
                yii::$app->session->setFlash('error','请上传封面');
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
            return $this->redirect(['index']);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    
    public function actionUpdateLive(){
        $model=new XcxApi();
        $info=$model->UpdateLive();
        if($info['status']==1){
          Yii::$app->session->setFlash('success','同步成功');
        }else{
            Yii::$app->session->setFlash('error','同步失败');
        }
        return $this->redirect(['index']);
    }
    /**
     * Deletes an existing Live model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        
        return $this->redirect(['index']);
    }
    
    /**
     * Finds the Live model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Live the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function findModel($id)
    {
        if (($model = Live::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
